package com.mingqijia.gassafety.shared.enums;

import com.mingqijia.gassafety.shared.constant.Constants;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Damon S.
 * @version v0.2
 * @date 2022-02-23 17:13
 */
@Getter
@RequiredArgsConstructor
public enum YesNoEnum {
    /** 1-是， 0-否 */
    NA(-1, false),
    N(0, false),
    Y(1, true);

    private final int code;
    private final boolean flag;

    public static YesNoEnum parse(int code) {
        List<YesNoEnum> filtered = Arrays.stream(YesNoEnum.values())
                .filter(item -> Objects.equals(item.getCode(), code))
                .collect(Collectors.toList());
        return CollectionUtils.isEmpty(filtered) ? NA : filtered.get(0);
    }

    public static YesNoEnum parse(boolean flag) {
        return flag ? Y : N;
    }

    /** on or off */
    public static YesNoEnum parse(String flag) {
        return Constants.STR_ON.equalsIgnoreCase(flag) ? Y : N;
    }
}
